package com.springboot.astra.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springboot.astra.bean.Postbody;
import com.springboot.astra.bean.Postmain;
import com.springboot.astra.bean.Reply;
import com.springboot.astra.bean.Theme;
import com.springboot.astra.result.DataResult;
import com.springboot.astra.service.PostbodyService;
import com.springboot.astra.service.PostmainService;
import com.springboot.astra.service.ReplyService;
import com.springboot.astra.service.ThemeService;
import com.springboot.astra.util.DateUtils;
import com.springboot.astra.util.JwtUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/post")
public class PostMainController {
    @Autowired
    JwtUtils jwtUtils;
    @Autowired
    ThemeService themeService;
    @Autowired
    PostmainService postmainService;
    @Autowired
    PostbodyService postbodyService;
    @Autowired
    ReplyService replyService;



    /**
     * 查找所有帖子
     * @return
     */
    @PostMapping("/getAllPostMain")
    public DataResult getAllPostMain(@RequestParam Map<String,String> map){
        //此次查询不需要获取帖子的主体，具体属性在点开详情里显示
        String query = map.get("query");
        Integer pagenum = Integer.valueOf(map.get("pagenum"));
        Integer pagesize = Integer.valueOf(map.get("pagesize"));
        Page<Postmain> page = new Page<Postmain>(pagenum,pagesize);
        IPage<Postmain> postMainList = postmainService.getAll(page,query);
        return DataResult.success(postMainList);
    }

    /**
     * 查找被举报帖子
     * @param map
     * @return
     */
    @PostMapping("/getAllIsReportPostMain")
    public DataResult getAllIsReportPostMain(@RequestParam Map<String,String> map){
        //此次查询不需要获取帖子的主体，具体属性在点开详情里显示
        String query = map.get("query");
        Integer pagenum = Integer.valueOf(map.get("pagenum"));
        Integer pagesize = Integer.valueOf(map.get("pagesize"));
        Page<Postmain> page = new Page<Postmain>(pagenum,pagesize);
        IPage<Postmain> postMainList = postmainService.getAllIsPostReport(page,query);
        return DataResult.success(postMainList);
    }

    /**
     * 查询一个帖子体
     * @param map
     * @return
     */
    @PostMapping("/getOnePostBody")
    public DataResult getOnePostBody(@RequestParam Map<String,String> map){
        Integer postbodyId = Integer.valueOf(map.get("postbodyId"));
        Postbody postbody = postbodyService.getOneById(postbodyId);
        return DataResult.success(postbody);
    }

    /**
     * 为用户查询所有帖子
     * @param map
     * @return
     */
    @PostMapping("/getAllPostForUser")
    public DataResult getAllPostForUser(@RequestParam Map<String,String> map){
        //此次查询不需要获取帖子的主体，具体属性在点开详情里显示
        String query = map.get("query");
        Integer pagenum = Integer.valueOf(map.get("pagenum"));
        Integer pagesize = Integer.valueOf(map.get("pagesize"));
        Page<Postmain> page = new Page<Postmain>(pagenum,pagesize);
        IPage<Postmain> postMainList = postmainService.getAllForUser(page,query);
        return DataResult.success(postMainList);
    }

    @PostMapping("/addPostFromUser")
    @Transactional
    public DataResult addPostFromUser(@RequestParam Map<String,String> map,HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        Claims claims = jwtUtils.parseJwt(token);
        Integer userId = Integer.valueOf(claims.getId());
        String themeName = map.get("themeName");
        String tittle = map.get("tittle");
        String body = map.get("body");
        Integer themeId = 0;
        List<Theme> themeList = themeService.getAll();
        for ( Theme t: themeList ) {
            if (t.getThemeName().equals(themeName)) {
                //注入主题id
                themeId = t.getThemeId();
                break;
            }
        }
        //注入postMain
        Postmain postmain = new Postmain();
        postmain.setUserId(userId);
        postmain.setPostTitle(tittle);
        postmain.setWeight(1);
        postmain.setCreateTime(DateUtils.getTime());
        postmain.setItTheme(themeId);
        postmain.setUpCount("0");
        postmain.setCanReply(true);
        postmain.setDelete(true);
        //插入数据
        postmainService.insertPostmain(postmain);
        Postbody postbody = new Postbody();
        postbody.setPostmainId(postmain.getPostmainId());
        postbody.setBody(body);
        postbody.setDelete(true);
        //插入帖体
        postbodyService.insertPostbody(postbody);
        return DataResult.success();
    }

    /**
     * 更新
     * @param postmain
     * @return
     */
    @PutMapping("/updateOnePostMain")
    public DataResult updateOnePostMain(@RequestBody Postmain postmain){
        postmainService.updateOneById(postmain);
        return DataResult.success();
    }

    /**
     * 举报帖子
     * @param map
     * @return
     */
    @PostMapping("/updateOnePostMainReport")
    public DataResult updateOnePostMainReport(@RequestParam Map<String,String> map){
        Integer postMainId = Integer.valueOf(map.get("postmainId"));
        postmainService.updatePostReport(postMainId);
        return DataResult.success();
    }
    @PutMapping("/updateOnePostBody")
    public DataResult updateOnePostBody(@RequestBody Postbody postbody){
        postbodyService.updateOneById(postbody);
        return DataResult.success();
    }


    /**
     * 删除---更改状态
     * @param postmain
     * @return
     */
    @PutMapping("/deleteOnePostMain")
    public DataResult deleteOnePostMain(@RequestBody Postmain postmain){
        postmainService.deleteOneById(postmain);
        return DataResult.success();
    }
    @PutMapping("/deleteOnePostBody")
    public DataResult deleteOnePostMain(@RequestBody Postbody postbody){
        postbodyService.deleteOneById(postbody);
        return DataResult.success();
    }

    /**
     * 操作主题
     * @param map
     * @return
     */
    @PostMapping("/getOneTheme")
    public DataResult getOneTag(@RequestParam Map<String,String> map){
        Theme theme = themeService.getOneByThemeId(Integer.valueOf(map.get("themeId")));
        return DataResult.success(theme);
    }

    /**
     * 为用户获取所有主题
     * @return
     */
    @GetMapping("/getAllThemeForUser")
    public DataResult getAllThemeForUser() {
        List<Theme> themeList = themeService.getAll();
        List<String> themeStrList = new ArrayList<String>();
        //只把名字放入数组
        for (Theme t:themeList) {
            themeStrList.add(t.getThemeName());
        }
        return DataResult.success(themeStrList);
    }
    @PostMapping("/getAllTheme")
    public DataResult getAllTag(@RequestParam Map<String,String> map){
        try {
            String query = map.get("query");
            Integer pagenum = Integer.valueOf(map.get("pagenum"));
            Integer pagesize = Integer.valueOf(map.get("pagesize"));
            Page<Theme> page = new Page<Theme>(pagenum, pagesize);
            IPage<Theme> theme= themeService.selectPageVo(page,query);
            return DataResult.success(theme);
        }catch (Exception e) {
            return DataResult.fail();
        }
    }
    //评论区

    /**
     * 为管理员查找所有评论
     * @param map
     * @return
     */
    @PostMapping("/getAllReply")
    public DataResult getAll(@RequestParam Map<String,String> map){
        String query = map.get("query");
        Integer pagenum = Integer.valueOf(map.get("pagenum"));
        Integer pagesize = Integer.valueOf(map.get("pagesize"));
        Page<Reply> page = new Page<Reply>(pagenum, pagesize);
        IPage<Reply> replyIPage = replyService.getAll(page,query);
        return DataResult.success(replyIPage);
    }

    /**
     * 查询所有被举报回复
     * @param map
     * @return
     */
    @PostMapping("/getAllIsReport")
    public DataResult getAllIsReport(@RequestParam Map<String,String> map){
        String query = map.get("query");
        Integer pagenum = Integer.valueOf(map.get("pagenum"));
        Integer pagesize = Integer.valueOf(map.get("pagesize"));
        Page<Reply> page = new Page<Reply>(pagenum, pagesize);
        IPage<Reply> replyIPage = replyService.getAllIsReport(page,query);
        return DataResult.success(replyIPage);
    }
    /**
     * 查询一个帖子的所有回复
     * @param map
     * @return
     */
    @PostMapping("/getAllForOnePostMain")
    public DataResult getAllForOnePostMain(@RequestParam Map<String,String> map) {
        Integer postmainId = Integer.valueOf(map.get("postmainId"));
        Integer pagenum = Integer.valueOf(map.get("pagenum"));
        Integer pagesize = Integer.valueOf(map.get("pagesize"));
        Page<Reply> page = new Page<Reply>(pagenum, pagesize);
        IPage<Reply> replies = replyService.getAllForUser(page,postmainId);
        return DataResult.success(replies);
    }

    /**
     * 获得一个用户的所有回复
     * @param map
     * @return
     */
    @PostMapping("/getAllReplyByUserId")
    public DataResult getAllReplyByUserId(@RequestParam Map<String,String> map,HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        Claims claims = jwtUtils.parseJwt(token);
        Integer userId = Integer.valueOf(claims.getId());
        Integer pagenum = Integer.valueOf(map.get("pagenum"));
        Integer pagesize = Integer.valueOf(map.get("pagesize"));
        Page<Reply> page = new Page<Reply>(pagenum, pagesize);
        IPage<Reply> replies = replyService.getAllReplyByUserId(page,userId);
        return DataResult.success(replies);
    }

    /**
     * 插入一个回复
     * @param reply
     * @return
     */
    @PutMapping("/addReply")
    public DataResult addReply(@RequestBody Reply reply, HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        Claims claims = jwtUtils.parseJwt(token);
        Integer userId = Integer.valueOf(claims.getId());
        reply.setUserId(userId);
        reply.setCreateTime(DateUtils.getTime());
        reply.setUpdateTime(DateUtils.getTime());
        reply.setDelete(true);
        replyService.insertReply(reply);
        return DataResult.success();
    }

    /**
     * 更新一条回复
     * @param reply
     * @return
     */
    @PutMapping("/updateReply")
    public DataResult updateReply(@RequestBody Reply reply){
        replyService.updateOneById(reply);
        return DataResult.success();
    }

    /**
     * 举报回复
     * @param
     * @return
     */
    @PostMapping("/reportReply")
    public DataResult reportReply(@RequestParam Map<String,String> map) {
        Integer replyId = Integer.valueOf(map.get("replyId"));
        replyService.reportReply(replyId);
        return DataResult.success();
    }

    /**
     * 更新一个回复
     * @param reply
     * @return
     */
    @PutMapping("/deleteReply")
    public DataResult deleteReply(@RequestBody Reply reply){
        replyService.deleteOneById(reply);
        return DataResult.success();
    }

    @PutMapping("/addTheme")
    public DataResult addTag(@RequestBody Theme theme){
        theme.setIsDelete(1);//1为可用，0为不可用
        themeService.insertOneTheme(theme);
        return DataResult.success();
    }

    @PutMapping("/updateTheme")
    public DataResult update(@RequestBody Theme theme){
        themeService.updateOneById(theme);
        return DataResult.success();
    }
}
